
clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3


********************************************************************************
*** DDD
********************************************************************************
destring udd_level, g(udd_kode)
g udd_kode20=(inlist(udd_kode,15,20,25))
g udd_kode35=(inlist(udd_kode,30,35))
g udd_kode40=(udd_kode==40)
g udd_kode50=(udd_kode==50)
g udd_kode60=(udd_kode>50)



** MAIN
cap drop help
cap drop interaction
g interaction=help3
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=help3*low_ses*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help3==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableC2.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 


* UDD20
cap drop help
cap drop interaction
cap drop help_20
g help=(max_p_20==1) if timing==1
bys pnr min_year: egen help_20=max(help)
g interaction=help_20
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=udd_kode20*help_20
g interaction3=udd_kode20*post
g ddd3=udd_kode20*help_20*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help_20==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_20 udd_kode20 interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableC2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 


*udd35
cap drop help
cap drop interaction
cap drop help_35
g help=(max_p_35==1) if timing==1
bys pnr min_year: egen help_35=max(help)
g interaction=help_35
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=udd_kode35*help_35
g interaction3=udd_kode35*post
g ddd3=udd_kode35*help_35*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help_35==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_35 udd_kode35 interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableC2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 

*udd40
cap drop help
cap drop interaction
cap drop help_40
g help=(max_p_40==1) if timing==1
bys pnr min_year: egen help_40=max(help)
g interaction=help_40
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=udd_kode40*help_40
g interaction3=udd_kode40*post
g ddd3=udd_kode40*help_40*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help_40==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_40 udd_kode40 interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableC2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 


*udd50
cap drop help
cap drop interaction
cap drop help_50
g help=(max_p_50==1) if timing==1
bys pnr min_year: egen help_50=max(help)
g interaction=help_50
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=udd_kode50*help_50
g interaction3=udd_kode50*post
g ddd3=udd_kode50*help_50*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help_50==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_50 udd_kode50 interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableC2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 


*udd60
cap drop help
cap drop interaction
cap drop help_60
g help=(max_p_60==1) if timing==1
bys pnr min_year: egen help_60=max(help)
g interaction=help_60
replace interaction=0 if post==0

cap drop interaction2 
cap drop interaction3 
cap drop ddd3
g interaction2=udd_kode60*help_60
g interaction3=udd_kode60*post
g ddd3=udd_kode60*help_60*post


sum death, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & help_60==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_60 udd_kode60 interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableC2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') 


